// pages/category/category.js
let {
  getCategory
} = require("../../api/category")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    conIndex: 0,
    itemIndex: 0,
    tab: [],
    content: [],
    tabs: [],
    viewid: '',
    scrollLeft: 0,
    topNum:0
  },
  // 切换效果
  setItem(event) {
    console.log(event);
    console.log(event.currentTarget.dataset.index);
    let index = event.currentTarget.dataset.index;
    this.setData({
      itemIndex: index
    })


    this.getNavData(index);
  },
  // 事件跳转路由
  onClick(event) {
    let type = event.currentTarget.dataset.type
    let id = event.currentTarget.dataset.id
    let name = event.currentTarget.dataset.name
    console.log(type, name, id);

    if (type == "product") {
      wx.navigateTo({
        url: "/pages/details/details?id=" + id,
      })
    } else {
      wx.navigateTo({
        url: "/pages/search/search?name=" + name,
      })
    }
  },


  // 头部选项
  setConIndex(event) {
    console.log(event.currentTarget.dataset.id);
    let index = event.currentTarget.dataset.id
    this.setData({
      viewid: "cont" + index,
      conIndex: index,
      scrollLeft: (index - 1) * 84
    })

  },
  // 左边tab选项
  getNavData(index) {
    console.log(index);

    getCategory().then(data => {
      let tab = data.data
      let tabs = tab.filter(item => {
        if (item.category_list) {
          return item
        }
      })
      console.log(tabs[index]);
      let listsArr = []
      let recom_list = tabs[index].category_list
      let listArr = []
      console.log(recom_list);
      for (var i = 0; i < tabs[index].title_list.length; i++) {
        listsArr = recom_list.filter(item => {
          return item.body.title == tabs[index].title_list[i].title
        })
        listArr.push(listsArr[0])
        console.log(listArr);
      }
      this.setData({
        topNum:0,
        "content": tabs[index],
        "tabs": listArr,
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log("是");
    getCategory().then(data => {
      console.log(data.data); 
      this.setData({
        "tab": data.data
      })
    })
    getCategory().then(data =>{
      console.log(data);
    })
    getCategory().then(data => {
      console.log(data);
      let tabs = data.data.filter(item => {
        if (item.category_list) {
          return item
        }
      })
      console.log(tabs);
      console.log(this.data.itemIndex);
      console.log(tabs[this.data.itemIndex]);
      let listsArr = []
      let listArr = []
      let recom_list = tabs[this.data.itemIndex].category_list

      console.log(tabs[this.data.itemIndex]);
      for (var i = 0; i < tabs[this.data.itemIndex].title_list.length; i++) {
        listsArr = recom_list.filter(item => {
          return item.body.title == tabs[this.data.itemIndex].title_list[i].title
        })
        listArr.push(listsArr[0])
        console.log(listArr);
      }
      console.log(tabs);
      this.setData({
        "tab": tabs,
        "tabs": listArr,
        "content": tabs[this.data.itemIndex],
      })
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
    let classId = wx.getStorageSync('classId');
    console.log(classId);
   
      if(classId > -1){
        this.getNavData(classId)
        this.setData({
          itemIndex:classId
        })
      }
    


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})